/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * ListUserManaged.java, 04/01/2013, bacdv
 */
package net.luvina.manageuser.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.model.SelectItem;

/**
 * CommonUtil
 *
 * @author bacdv
 *
 */
public class CommonUtil {
    private static final int TOTAL_MONTH_IN_YEAR = 12;
    private static final int TOTAL_DAY_IN_MONTH = 31;
    private static final int FROM_YEAR = 1900;

    /**
     * convertToString
     *
     * @param year
     *            year
     * @param month
     *            month
     * @param day
     *            day
     * @return string date
     */
    public static String convertToString(int year, int month, int day) {
        StringBuilder sbResult = new StringBuilder();
        String slash = "/";
        sbResult.append(year).append(slash).append(month).append(slash).append(
                day);
        if (sbResult != null) {
            return sbResult.toString();
        } else {
            return null;
        }
    }

    /**
     * Convert Year,Month,Day to Date
     *
     * @param year
     *            year
     * @param month
     *            month
     * @param day
     *            day
     * @return Date date
     */
    public static Date toDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DATE, day);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * toArrayInteger
     *
     * @param date
     *            date
     * @return arrayInteger
     */
    public static List<Integer> toArrayInteger(Date date) {
        if (date == null) {
            return null;
        }
        List<Integer> result = new ArrayList<Integer>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        result.add(cal.get(Calendar.YEAR));
        result.add(cal.get(Calendar.MONTH) + 1);
        result.add(cal.get(Calendar.DATE));
        return result;
    }

    /**
     * getListSelectItemDay
     *
     * @return list item day
     */
    public static List<SelectItem> getListSelectItemDay() {
        List<SelectItem> listSelectItemDay = new ArrayList<SelectItem>();
        for (int day = 1; day <= TOTAL_DAY_IN_MONTH; day++) {
            listSelectItemDay.add(new SelectItem(Integer.toString(day), Integer
                    .toString(day)));
        }
        return listSelectItemDay;
    }

    /**
     * getListSelectItemMonth
     *
     * @return list item month
     */
    public static List<SelectItem> getListSelectItemMonth() {
        List<SelectItem> listSelectItemMonth = new ArrayList<SelectItem>();
        for (int month = 1; month <= TOTAL_MONTH_IN_YEAR; month++) {
            listSelectItemMonth.add(new SelectItem(Integer.toString(month),
                    Integer.toString(month)));
        }
        return listSelectItemMonth;
    }

    /**
     * getListSelectItemYear
     *
     * @return list item year
     */
    public static List<SelectItem> getListSelectItemYear() {
        int nowYear = getYearNow();
        List<SelectItem> listSelectItemYear = new ArrayList<SelectItem>();
        for (int year = FROM_YEAR; year <= nowYear; year++) {
            listSelectItemYear.add(new SelectItem(Integer.toString(year),
                    Integer.toString(year)));
        }
        return listSelectItemYear;
    }

    /**
     * getYearNow
     *
     * @return now year
     */
    public static int getYearNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        int year = Integer.parseInt(sdf.format(date));
        return year;
    }

    /**
     *
     * @param c
     *            char
     * @return true if c is char/ false if c not char
     */
    public static boolean isDigit(char c) {
        int x = (int) c;
        if ((x >= 48) && (x <= 57)) {
            return true;
        }
        return false;
    }

    /**
     * check null string
     *
     * @param str
     *            string
     * @return true if str is null/ false if str not null
     */
    public static boolean isNull(String str) {
        return (str == null || str.length() <= 0);
    }

    /**
     *
     * @param number
     *            string
     * @return true if number is number/ false if number is not number
     */
    public static boolean isNumber(String number) {
        if (isNull(number)) {
            return true;
        }
        char[] c = number.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (!isDigit(c[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * Converter String to Integer
     *
     * @param value
     *            String
     * @return Integer
     */
    public static Integer toInteger(String value) {
        if ((null == value) || ("".equals(value)) || (!isNumber(value))) {
            return 0;
        } else {
            return new Integer(value);
        }
    }

    /**
     * getLimit
     *
     * @return limit get records of one page
     */
    public static int getLimit() {
        int limit = Integer.parseInt(ConfigProperties.getData("limit"));
        return limit;
    }

    /**
     * getOffset
     *
     * @param currentPage
     *            currentPage
     * @param limit
     *            limit
     * @return offset offset
     */
    public static int getOffset(int currentPage, int limit) {
        int offset = (currentPage > 0) ? limit * ((int) currentPage - 1) : 0;
        return offset;
    }

    /**
     * getTotalPage
     *
     * @param totalRecords
     *            total records
     * @param limit
     *            records of one page
     * @return total total page
     */
    public static int getTotalPage(int totalRecords, int limit) {
        int total;
        if ((totalRecords % limit) == 0) {
            total = (totalRecords / limit);
        } else {
            total = (totalRecords / limit) + 1;
        }
        return total;
    }

    /**
     * paging
     *
     * @param totalRecords
     *            totalRecords
     * @param limit
     *            limit
     * @param currentPage
     *            currentPage
     * @param actionName
     *            actionName
     * @return paging paging
     */
    public static String paging(int totalRecords, int limit, int currentPage,
            String actionName) {
        int page_range = Integer
                .valueOf(ConfigProperties.getData("page_range"));
        String str = "";
        String separator = "";
        String first_str = "&laquo;";
        String last_str = "&raquo;";
        int next;
        int prev;
        int totalPage = CommonUtil.getTotalPage(totalRecords, limit);
        if (totalPage == 0) {
            return str;
        }
        String cc;
        if (actionName.indexOf("?") > -1) {
            cc = "&";
        } else {
            cc = "?";
        }

        currentPage = (int) currentPage;

        if (currentPage < 1) {
            currentPage = 1;
        }

        if (currentPage > totalPage) {
            currentPage = totalPage;
        }

        if (totalPage < page_range) {
            page_range = totalPage;
        }

        int division;
        int page_lim1 = 0;
        int page_lim2 = 0;

        if (page_range == 0) {
            division = 0;
        } else {
            division = (int) Math.floor(totalPage / page_range);
        }

        for (int i = 0; i <= division; i++) {
            if (((i * page_range + 1) <= currentPage)
                    && (((i + 1) * page_range) >= currentPage)) {
                page_lim1 = i * page_range + 1;
                page_lim2 = page_lim1 + page_range - 1;
                if (page_lim2 > totalPage) {
                    page_lim2 = totalPage;
                }
                break;
            }
        }

        if (currentPage % 3 == 0) {
            next = currentPage + 1;
            prev = currentPage - 5;
        } else if (currentPage % 3 == 2) {
            next = currentPage + 2;
            prev = currentPage - 4;
        } else {
            next = currentPage + 3;
            prev = currentPage - 3;
        }

        if (totalPage > page_range) {
            if (currentPage > page_range) {
                if (first_str.trim().compareTo("") != 0)
                    str += " <a href='" + actionName + ".do" + cc + "page="
                            + prev + "' class='txtBlue'>" + first_str
                            + "&nbsp;</a>";
            }
        }

        for (int i = page_lim1; i < currentPage; i++) {
            str += separator + "<a href='" + actionName + ".do" + cc + "page="
                    + i + "' class='txtBlue'>" + i + "</a> ";
        }

        str += separator + "<span class='txtGrey11'>" + currentPage
                + "&nbsp;</span>";

        for (int i = currentPage + 1; i <= page_lim2; i++) {
            str += separator + "<a href='" + actionName + ".do" + cc + "page="
                    + i + "' class='txtBlue'>" + (i) + "</a> ";
        }
        str += separator;

        if (totalPage > 1) {
            if (next <= totalPage) {
                if (last_str.trim().compareTo("") != 0)
                    str += "  <a href='" + actionName + ".do" + cc + "page="
                            + next + "' class='txtBlue'>" + last_str + "</a>";
            }
        } else {
            str = "";
        }

        return str;
    }
}
